/*
* Copyright 2004-2010 mb_sx@hotmail.com
* All Rights Reserved.
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Collections.Generic;
using System.IO;
using DeskNotes.Utils;

namespace DeskNotes
{
	/// <summary>
	/// Display archived desknotes in hierarchy organized by categories.
	/// </summary>
	public class ArchiveForm : System.Windows.Forms.Form
	{
        internal static int COLLAPSED_IMAGE = 0;
		internal static int EXPANDED_IMAGE = 1;
        internal static int COLLAPSED_CHANGED_IMAGE = 2;
        internal static int EXPANDED_CHANGED_IMAGE = 3;
		internal static int NOTE_IMAGE = 4;
        internal static int NOTE_CHANGED_IMAGE = 5;
		
		private bool currentNoteChanged = false;
        private bool searchMarksOn = false;
        private bool ignoreModificationAfterMarks = false;
        private bool allowToMarkModifications = false;
        private bool searchListUsed = false;
		private TreeNode selectedNode = null;
		internal DeskNotesData deskNotes;
        internal SearchableArchive index;
        private string wndTitleModified, wndTitleUnmodified;

        private Rectangle dragBoxFromMouseDown = Rectangle.Empty;

        private Color backColor, textColor;
        internal System.Windows.Forms.ImageList imgListTree;
        private System.Windows.Forms.ToolTip toolTip;
        private FontDialog fontDialog;
        private ColorDialog colorDialog;
        private ContextMenuStrip ctxMenuArchNote;
        private ToolStripMenuItem toolbarStripMenuItem;
        private ToolStripMenuItem statusbarStripMenuItem;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripMenuItem printStripMenuItem;
        private ContextMenuStrip ctxMenuCategory;
        private ToolStripMenuItem newCategoryStripMenuItem;
        private ToolStripMenuItem removeStripMenuItem;
        private ToolStripSeparator toolStripSeparator7;
        private ToolStripMenuItem printPreviewStripMenuItem;
        private ToolStripMenuItem printCategoryStripMenuItem;
        private ContextMenuStrip ctxMenuTreeNote;
        private ToolStripMenuItem moveToDesktopStripMenuItem;
        private ToolStripMenuItem copyToDesktopStripMenuItem;
        private ToolStripSeparator toolStripSeparator8;
        private ToolStripMenuItem removeNoteStripMenuItem;
        private ToolStripSeparator toolStripSeparator9;
        private ToolStripMenuItem printToolStripMenuItem;
        private SplitContainer splitContainer1;
        private TabControl tabControl1;
        private TabPage tabArchive;
        private TreeView treeArchive;
        private TabPage tabSearch;
        private ToolStripContainer toolStripContainer1;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel lblArch;
        private ToolStripStatusLabel lblArchivedTime;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripStatusLabel lblMod;
        private ToolStripStatusLabel lblModificationTime;
        internal ToolStripStatusLabel lblIndexing;
        internal ToolStripProgressBar indexProgress;
        private Panel panel1;
        private RichTextBox txtNote;
        private Label label1;
        private ToolStrip toolStripRTF;
        private ToolStripButton btnSave;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripButton btnBullet;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripButton btnBold;
        private ToolStripButton btnItalic;
        private ToolStripButton btnStrike;
        private ToolStripButton btnUnderline;
        private ToolStripSeparator toolStripSeparator5;
        private ToolStripButton btnIndentRight;
        private ToolStripSeparator toolStripSeparator6;
        private ToolStripButton btnFont;
        private ToolStripButton btnSelectionBackColor;
        private TableLayoutPanel tableLayoutPanel1;
        private ListView listSearch;
        private Panel panel2;
        private PictureBox picSearch;
        private TextBox txtSearch;
        private ColumnHeader colNote;
        private ColumnHeader colCategory;
        private MenuStrip menuStrip1;
        private ToolStripMenuItem archiveToolStripMenuItem;
        private ToolStripMenuItem mnuSave;
        private ToolStripSeparator toolStripSeparator10;
        private ToolStripMenuItem mnuExit;
        private ToolStripButton btnNoteBackColor;
        private HelpProvider helpProvider1;
        private ToolStripButton btnNumbered;
		private System.ComponentModel.IContainer components;
				

		/// <summary>
		/// DeskNotes property Notes can be set only once for ArchiveForm
		/// It is done to prevent desknotes and archive to be accidentally changed
		/// by different forms
		/// </summary>
		internal DeskNotesData Notes
		{
			set 
			{
				if (null == deskNotes)
					deskNotes = value;
			}
		}

		/// <summary>
		/// Changes forms UI to indicate note or category was changed.
		/// Called when note is archived, modified or removed. 
		/// Also when category is added, renamed and removed.
		/// </summary>
		private void NeedSave()
		{
            if (allowToMarkModifications)
            {
                btnSave.Enabled = true;
                deskNotes.archiveModified = true;
                this.Text = wndTitleModified;
            }
		}

		/// <summary>
		/// When archive was saved then reset UI to indicate that.
		/// </summary>
		private void ArchiveSaved()
		{
			foreach(TreeNode node in treeArchive.Nodes)
                ((DeskNotesTreeNode)node).ResetEdited();

			btnSave.Enabled = false;
            deskNotes.archiveModified = false;
            this.Text = wndTitleUnmodified;
		}


		public ArchiveForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
           
		}

		/// <summary>
		/// Creates archive when called from Note's menu or from sys tray icon menu.
		/// </summary>
		/// <param name="notes">DeskNotes handling archive dataset.</param>
		internal ArchiveForm(DeskNotesData notes) : this()
		{
            Properties.Settings settings = Properties.Settings.Default;
            if (0 != settings.ArchivePosition.Width)
            {
                Rectangle position = settings.ArchivePosition;
                this.StartPosition = FormStartPosition.Manual;
                this.Left = position.X;
                this.Top = position.Y;
                this.Width = position.Width;
                this.Height = position.Height;
            }
            if (-1 != settings.ArchiveSplitter)
            {
                this.splitContainer1.SplitterDistance = settings.ArchiveSplitter;
            }
			this.Notes = notes;
			notes.archiveForm = this;
            wndTitleModified = this.Text + " *";
            wndTitleUnmodified = this.Text;
            // Extra form properties intialization not possible in VS Properties Window
			this.Disposed += new EventHandler(notes.archiveForm_Disposed);
            txtNote.AllowDrop = true;
            txtNote.DragEnter += new DragEventHandler(txtNote_DragEnter);
            txtNote.DragDrop += new DragEventHandler(txtNote_DragDrop);
            lblIndexing.Visible = false;
            indexProgress.Visible = false;

		}

        /// <summary>
        /// Archive is loaded from DataSet into tree structure and assigned 
        /// into treeview treeArchive
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ArchiveForm_Load(object sender, System.EventArgs e)
        {
            TreeNode sNode = new TreeNode();
            treeArchive.Nodes.Add(deskNotes.CreateTreeFromArchive(ref sNode));
            txtNote.Font = Properties.Settings.Default.Font;
            textColor = txtNote.ForeColor;
            backColor = txtNote.BackColor;
            treeArchive.SelectedNode = sNode;
            allowToMarkModifications = true;

            index = new SearchableArchive();
            Properties.Settings settings = Properties.Settings.Default;
            index.Filename = settings.SearchableArchiveUrl;
            // open the searchableArchive index which was updated during start of DeskNotes
            index.Open();
            treeArchive.Focus();
        }

        
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

        
        //[System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ArchiveForm));
            System.Windows.Forms.ToolStripMenuItem insertTimeStripMenuItem;
            System.Windows.Forms.ToolStripButton btnIndentLeft;
            this.toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.lblArch = new System.Windows.Forms.ToolStripStatusLabel();
            this.lblArchivedTime = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.lblMod = new System.Windows.Forms.ToolStripStatusLabel();
            this.lblModificationTime = new System.Windows.Forms.ToolStripStatusLabel();
            this.lblIndexing = new System.Windows.Forms.ToolStripStatusLabel();
            this.indexProgress = new System.Windows.Forms.ToolStripProgressBar();
            this.panel1 = new System.Windows.Forms.Panel();
            this.txtNote = new System.Windows.Forms.RichTextBox();
            this.ctxMenuArchNote = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.toolbarStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.statusbarStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.printStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.label1 = new System.Windows.Forms.Label();
            this.toolStripRTF = new System.Windows.Forms.ToolStrip();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnBullet = new System.Windows.Forms.ToolStripButton();
            this.btnNumbered = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.btnBold = new System.Windows.Forms.ToolStripButton();
            this.btnItalic = new System.Windows.Forms.ToolStripButton();
            this.btnStrike = new System.Windows.Forms.ToolStripButton();
            this.btnUnderline = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.btnIndentRight = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
            this.btnFont = new System.Windows.Forms.ToolStripButton();
            this.btnSelectionBackColor = new System.Windows.Forms.ToolStripButton();
            this.btnNoteBackColor = new System.Windows.Forms.ToolStripButton();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabArchive = new System.Windows.Forms.TabPage();
            this.treeArchive = new System.Windows.Forms.TreeView();
            this.imgListTree = new System.Windows.Forms.ImageList(this.components);
            this.tabSearch = new System.Windows.Forms.TabPage();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.listSearch = new System.Windows.Forms.ListView();
            this.colNote = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.colCategory = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.panel2 = new System.Windows.Forms.Panel();
            this.picSearch = new System.Windows.Forms.PictureBox();
            this.txtSearch = new System.Windows.Forms.TextBox();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.ctxMenuCategory = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.newCategoryStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.removeStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator7 = new System.Windows.Forms.ToolStripSeparator();
            this.printPreviewStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.printCategoryStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.ctxMenuTreeNote = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.moveToDesktopStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.copyToDesktopStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator8 = new System.Windows.Forms.ToolStripSeparator();
            this.removeNoteStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator();
            this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.archiveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuSave = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator10 = new System.Windows.Forms.ToolStripSeparator();
            this.mnuExit = new System.Windows.Forms.ToolStripMenuItem();
            this.fontDialog = new System.Windows.Forms.FontDialog();
            this.colorDialog = new System.Windows.Forms.ColorDialog();
            this.helpProvider1 = new System.Windows.Forms.HelpProvider();
            insertTimeStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            btnIndentLeft = new System.Windows.Forms.ToolStripButton();
            this.toolStripContainer1.BottomToolStripPanel.SuspendLayout();
            this.toolStripContainer1.ContentPanel.SuspendLayout();
            this.toolStripContainer1.TopToolStripPanel.SuspendLayout();
            this.toolStripContainer1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.ctxMenuArchNote.SuspendLayout();
            this.toolStripRTF.SuspendLayout();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabArchive.SuspendLayout();
            this.tabSearch.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.picSearch)).BeginInit();
            this.ctxMenuCategory.SuspendLayout();
            this.ctxMenuTreeNote.SuspendLayout();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStripContainer1
            // 
            // 
            // toolStripContainer1.BottomToolStripPanel
            // 
            this.toolStripContainer1.BottomToolStripPanel.Controls.Add(this.statusStrip1);
            this.helpProvider1.SetShowHelp(this.toolStripContainer1.BottomToolStripPanel, ((bool)(resources.GetObject("toolStripContainer1.BottomToolStripPanel.ShowHelp"))));
            // 
            // toolStripContainer1.ContentPanel
            // 
            this.toolStripContainer1.ContentPanel.Controls.Add(this.panel1);
            this.helpProvider1.SetShowHelp(this.toolStripContainer1.ContentPanel, ((bool)(resources.GetObject("toolStripContainer1.ContentPanel.ShowHelp"))));
            resources.ApplyResources(this.toolStripContainer1.ContentPanel, "toolStripContainer1.ContentPanel");
            resources.ApplyResources(this.toolStripContainer1, "toolStripContainer1");
            // 
            // toolStripContainer1.LeftToolStripPanel
            // 
            this.helpProvider1.SetShowHelp(this.toolStripContainer1.LeftToolStripPanel, ((bool)(resources.GetObject("toolStripContainer1.LeftToolStripPanel.ShowHelp"))));
            this.toolStripContainer1.Name = "toolStripContainer1";
            // 
            // toolStripContainer1.RightToolStripPanel
            // 
            this.helpProvider1.SetShowHelp(this.toolStripContainer1.RightToolStripPanel, ((bool)(resources.GetObject("toolStripContainer1.RightToolStripPanel.ShowHelp"))));
            this.helpProvider1.SetShowHelp(this.toolStripContainer1, ((bool)(resources.GetObject("toolStripContainer1.ShowHelp"))));
            // 
            // toolStripContainer1.TopToolStripPanel
            // 
            this.toolStripContainer1.TopToolStripPanel.Controls.Add(this.toolStripRTF);
            this.helpProvider1.SetShowHelp(this.toolStripContainer1.TopToolStripPanel, ((bool)(resources.GetObject("toolStripContainer1.TopToolStripPanel.ShowHelp"))));
            // 
            // statusStrip1
            // 
            resources.ApplyResources(this.statusStrip1, "statusStrip1");
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.lblArch,
            this.lblArchivedTime,
            this.toolStripSeparator2,
            this.lblMod,
            this.lblModificationTime,
            this.lblIndexing,
            this.indexProgress});
            this.statusStrip1.Name = "statusStrip1";
            this.helpProvider1.SetShowHelp(this.statusStrip1, ((bool)(resources.GetObject("statusStrip1.ShowHelp"))));
            // 
            // lblArch
            // 
            this.lblArch.Name = "lblArch";
            resources.ApplyResources(this.lblArch, "lblArch");
            // 
            // lblArchivedTime
            // 
            this.lblArchivedTime.BorderStyle = System.Windows.Forms.Border3DStyle.Etched;
            this.lblArchivedTime.Name = "lblArchivedTime";
            resources.ApplyResources(this.lblArchivedTime, "lblArchivedTime");
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            resources.ApplyResources(this.toolStripSeparator2, "toolStripSeparator2");
            // 
            // lblMod
            // 
            this.lblMod.Name = "lblMod";
            resources.ApplyResources(this.lblMod, "lblMod");
            // 
            // lblModificationTime
            // 
            this.lblModificationTime.BorderStyle = System.Windows.Forms.Border3DStyle.Etched;
            this.lblModificationTime.Name = "lblModificationTime";
            resources.ApplyResources(this.lblModificationTime, "lblModificationTime");
            // 
            // lblIndexing
            // 
            this.lblIndexing.Name = "lblIndexing";
            this.lblIndexing.Overflow = System.Windows.Forms.ToolStripItemOverflow.Always;
            resources.ApplyResources(this.lblIndexing, "lblIndexing");
            // 
            // indexProgress
            // 
            this.indexProgress.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            resources.ApplyResources(this.indexProgress, "indexProgress");
            this.indexProgress.Name = "indexProgress";
            this.indexProgress.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never;
            this.indexProgress.Step = 1;
            this.indexProgress.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.txtNote);
            this.panel1.Controls.Add(this.label1);
            resources.ApplyResources(this.panel1, "panel1");
            this.panel1.Name = "panel1";
            this.helpProvider1.SetShowHelp(this.panel1, ((bool)(resources.GetObject("panel1.ShowHelp"))));
            // 
            // txtNote
            // 
            this.txtNote.AcceptsTab = true;
            this.txtNote.AutoWordSelection = true;
            this.txtNote.BackColor = System.Drawing.Color.White;
            resources.ApplyResources(this.txtNote, "txtNote");
            this.txtNote.ContextMenuStrip = this.ctxMenuArchNote;
            this.txtNote.HideSelection = false;
            this.txtNote.Name = "txtNote";
            this.helpProvider1.SetShowHelp(this.txtNote, ((bool)(resources.GetObject("txtNote.ShowHelp"))));
            this.toolTip.SetToolTip(this.txtNote, resources.GetString("txtNote.ToolTip"));
            this.txtNote.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.txtNote_LinkClicked);
            this.txtNote.SelectionChanged += new System.EventHandler(this.txtNote_SelectionChanged);
            this.txtNote.Enter += new System.EventHandler(this.txtNote_Enter);
            this.txtNote.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtNote_KeyDown);
            this.txtNote.Leave += new System.EventHandler(this.txtNote_Leave);
            this.txtNote.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtNote_MouseDown);
            this.txtNote.MouseMove += new System.Windows.Forms.MouseEventHandler(this.txtNote_MouseMove);
            this.txtNote.MouseUp += new System.Windows.Forms.MouseEventHandler(this.txtNote_MouseUp);
            // 
            // ctxMenuArchNote
            // 
            this.ctxMenuArchNote.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolbarStripMenuItem,
            this.statusbarStripMenuItem,
            this.toolStripSeparator3,
            insertTimeStripMenuItem,
            this.printStripMenuItem});
            this.ctxMenuArchNote.Name = "ctxMenuArchNote";
            this.helpProvider1.SetShowHelp(this.ctxMenuArchNote, ((bool)(resources.GetObject("ctxMenuArchNote.ShowHelp"))));
            resources.ApplyResources(this.ctxMenuArchNote, "ctxMenuArchNote");
            this.ctxMenuArchNote.Opening += new System.ComponentModel.CancelEventHandler(this.ctxMenuArchNote_Opening);
            // 
            // toolbarStripMenuItem
            // 
            this.toolbarStripMenuItem.CheckOnClick = true;
            this.toolbarStripMenuItem.Name = "toolbarStripMenuItem";
            resources.ApplyResources(this.toolbarStripMenuItem, "toolbarStripMenuItem");
            this.toolbarStripMenuItem.Click += new System.EventHandler(this.toolbarStripMenuItem_Click);
            // 
            // statusbarStripMenuItem
            // 
            this.statusbarStripMenuItem.Name = "statusbarStripMenuItem";
            resources.ApplyResources(this.statusbarStripMenuItem, "statusbarStripMenuItem");
            this.statusbarStripMenuItem.Click += new System.EventHandler(this.statusbarStripMenuItem_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            resources.ApplyResources(this.toolStripSeparator3, "toolStripSeparator3");
            // 
            // insertTimeStripMenuItem
            // 
            insertTimeStripMenuItem.Name = "insertTimeStripMenuItem";
            resources.ApplyResources(insertTimeStripMenuItem, "insertTimeStripMenuItem");
            insertTimeStripMenuItem.Click += new System.EventHandler(this.insertTimeStripMenuItem_Click);
            // 
            // printStripMenuItem
            // 
            resources.ApplyResources(this.printStripMenuItem, "printStripMenuItem");
            this.printStripMenuItem.Name = "printStripMenuItem";
            this.printStripMenuItem.Click += new System.EventHandler(this.printStripMenuItem_Click);
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            this.helpProvider1.SetShowHelp(this.label1, ((bool)(resources.GetObject("label1.ShowHelp"))));
            // 
            // toolStripRTF
            // 
            this.toolStripRTF.AllowItemReorder = true;
            resources.ApplyResources(this.toolStripRTF, "toolStripRTF");
            this.toolStripRTF.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnSave,
            this.toolStripSeparator1,
            this.btnBullet,
            this.btnNumbered,
            this.toolStripSeparator4,
            this.btnBold,
            this.btnItalic,
            this.btnStrike,
            this.btnUnderline,
            this.toolStripSeparator5,
            this.btnIndentRight,
            btnIndentLeft,
            this.toolStripSeparator6,
            this.btnFont,
            this.btnSelectionBackColor,
            this.btnNoteBackColor});
            this.toolStripRTF.Name = "toolStripRTF";
            this.helpProvider1.SetShowHelp(this.toolStripRTF, ((bool)(resources.GetObject("toolStripRTF.ShowHelp"))));
            this.toolStripRTF.Stretch = true;
            // 
            // btnSave
            // 
            this.btnSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnSave, "btnSave");
            this.btnSave.Name = "btnSave";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            resources.ApplyResources(this.toolStripSeparator1, "toolStripSeparator1");
            // 
            // btnBullet
            // 
            this.btnBullet.CheckOnClick = true;
            this.btnBullet.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnBullet, "btnBullet");
            this.btnBullet.Name = "btnBullet";
            this.btnBullet.Click += new System.EventHandler(this.btnBullet_Click);
            // 
            // btnNumbered
            // 
            this.btnNumbered.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnNumbered, "btnNumbered");
            this.btnNumbered.Name = "btnNumbered";
            this.btnNumbered.Click += new System.EventHandler(this.btnNumbered_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            resources.ApplyResources(this.toolStripSeparator4, "toolStripSeparator4");
            // 
            // btnBold
            // 
            this.btnBold.CheckOnClick = true;
            this.btnBold.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnBold, "btnBold");
            this.btnBold.Name = "btnBold";
            this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
            // 
            // btnItalic
            // 
            this.btnItalic.CheckOnClick = true;
            this.btnItalic.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnItalic, "btnItalic");
            this.btnItalic.Name = "btnItalic";
            this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
            // 
            // btnStrike
            // 
            this.btnStrike.CheckOnClick = true;
            this.btnStrike.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnStrike, "btnStrike");
            this.btnStrike.Name = "btnStrike";
            this.btnStrike.Click += new System.EventHandler(this.btnStrike_Click);
            // 
            // btnUnderline
            // 
            this.btnUnderline.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnUnderline, "btnUnderline");
            this.btnUnderline.Name = "btnUnderline";
            this.btnUnderline.Click += new System.EventHandler(this.btnUnderline_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            resources.ApplyResources(this.toolStripSeparator5, "toolStripSeparator5");
            // 
            // btnIndentRight
            // 
            this.btnIndentRight.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnIndentRight, "btnIndentRight");
            this.btnIndentRight.Name = "btnIndentRight";
            this.btnIndentRight.Click += new System.EventHandler(this.btnIndentRight_Click);
            // 
            // btnIndentLeft
            // 
            btnIndentLeft.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(btnIndentLeft, "btnIndentLeft");
            btnIndentLeft.Name = "btnIndentLeft";
            btnIndentLeft.Click += new System.EventHandler(this.btnIndentLeft_Click);
            // 
            // toolStripSeparator6
            // 
            this.toolStripSeparator6.Name = "toolStripSeparator6";
            resources.ApplyResources(this.toolStripSeparator6, "toolStripSeparator6");
            // 
            // btnFont
            // 
            this.btnFont.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnFont, "btnFont");
            this.btnFont.Name = "btnFont";
            this.btnFont.Click += new System.EventHandler(this.btnFont_Click);
            // 
            // btnSelectionBackColor
            // 
            this.btnSelectionBackColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnSelectionBackColor, "btnSelectionBackColor");
            this.btnSelectionBackColor.Name = "btnSelectionBackColor";
            this.btnSelectionBackColor.Click += new System.EventHandler(this.btnSelectionBackColor_Click);
            // 
            // btnNoteBackColor
            // 
            this.btnNoteBackColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.btnNoteBackColor.Image = global::DeskNotes.Properties.Resources.BackColor;
            resources.ApplyResources(this.btnNoteBackColor, "btnNoteBackColor");
            this.btnNoteBackColor.Name = "btnNoteBackColor";
            this.btnNoteBackColor.Click += new System.EventHandler(this.btnNoteBackColor_Click);
            // 
            // splitContainer1
            // 
            resources.ApplyResources(this.splitContainer1, "splitContainer1");
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.tabControl1);
            this.helpProvider1.SetShowHelp(this.splitContainer1.Panel1, ((bool)(resources.GetObject("splitContainer1.Panel1.ShowHelp"))));
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.toolStripContainer1);
            this.helpProvider1.SetShowHelp(this.splitContainer1.Panel2, ((bool)(resources.GetObject("splitContainer1.Panel2.ShowHelp"))));
            this.helpProvider1.SetShowHelp(this.splitContainer1, ((bool)(resources.GetObject("splitContainer1.ShowHelp"))));
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabArchive);
            this.tabControl1.Controls.Add(this.tabSearch);
            resources.ApplyResources(this.tabControl1, "tabControl1");
            this.helpProvider1.SetHelpKeyword(this.tabControl1, resources.GetString("tabControl1.HelpKeyword"));
            this.helpProvider1.SetHelpNavigator(this.tabControl1, ((System.Windows.Forms.HelpNavigator)(resources.GetObject("tabControl1.HelpNavigator"))));
            this.tabControl1.HotTrack = true;
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.helpProvider1.SetShowHelp(this.tabControl1, ((bool)(resources.GetObject("tabControl1.ShowHelp"))));
            this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);
            // 
            // tabArchive
            // 
            this.tabArchive.Controls.Add(this.treeArchive);
            resources.ApplyResources(this.tabArchive, "tabArchive");
            this.tabArchive.Name = "tabArchive";
            this.helpProvider1.SetShowHelp(this.tabArchive, ((bool)(resources.GetObject("tabArchive.ShowHelp"))));
            this.toolTip.SetToolTip(this.tabArchive, resources.GetString("tabArchive.ToolTip"));
            this.tabArchive.UseVisualStyleBackColor = true;
            // 
            // treeArchive
            // 
            this.treeArchive.AllowDrop = true;
            resources.ApplyResources(this.treeArchive, "treeArchive");
            this.treeArchive.HotTracking = true;
            this.treeArchive.ImageList = this.imgListTree;
            this.treeArchive.ItemHeight = 16;
            this.treeArchive.LabelEdit = true;
            this.treeArchive.Name = "treeArchive";
            this.helpProvider1.SetShowHelp(this.treeArchive, ((bool)(resources.GetObject("treeArchive.ShowHelp"))));
            this.toolTip.SetToolTip(this.treeArchive, resources.GetString("treeArchive.ToolTip"));
            this.treeArchive.BeforeLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.treeArchive_BeforeLabelEdit);
            this.treeArchive.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.treeArchive_AfterLabelEdit);
            this.treeArchive.AfterCollapse += new System.Windows.Forms.TreeViewEventHandler(this.treeArchive_AfterCollapse);
            this.treeArchive.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeArchive_BeforeExpand);
            this.treeArchive.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeArchive_AfterExpand);
            this.treeArchive.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeArchive_ItemDrag);
            this.treeArchive.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeArchive_BeforeSelect);
            this.treeArchive.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeArchive_AfterSelect);
            this.treeArchive.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragDrop);
            this.treeArchive.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragEnter);
            this.treeArchive.DragOver += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragOver);
            this.treeArchive.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ArchiveForm_KeyDown);
            this.treeArchive.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeArchive_MouseDown);
            this.treeArchive.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeArchive_MouseUp);
            // 
            // imgListTree
            // 
            this.imgListTree.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgListTree.ImageStream")));
            this.imgListTree.TransparentColor = System.Drawing.Color.Transparent;
            this.imgListTree.Images.SetKeyName(0, "Collapsed");
            this.imgListTree.Images.SetKeyName(1, "Expanded");
            this.imgListTree.Images.SetKeyName(2, "CollapsedMOd");
            this.imgListTree.Images.SetKeyName(3, "ExpandedMod");
            this.imgListTree.Images.SetKeyName(4, "Note");
            this.imgListTree.Images.SetKeyName(5, "NoteMod");
            // 
            // tabSearch
            // 
            this.tabSearch.Controls.Add(this.tableLayoutPanel1);
            resources.ApplyResources(this.tabSearch, "tabSearch");
            this.tabSearch.Name = "tabSearch";
            this.helpProvider1.SetShowHelp(this.tabSearch, ((bool)(resources.GetObject("tabSearch.ShowHelp"))));
            this.toolTip.SetToolTip(this.tabSearch, resources.GetString("tabSearch.ToolTip"));
            this.tabSearch.UseVisualStyleBackColor = true;
            // 
            // tableLayoutPanel1
            // 
            resources.ApplyResources(this.tableLayoutPanel1, "tableLayoutPanel1");
            this.tableLayoutPanel1.Controls.Add(this.listSearch, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.panel2, 0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.helpProvider1.SetShowHelp(this.tableLayoutPanel1, ((bool)(resources.GetObject("tableLayoutPanel1.ShowHelp"))));
            // 
            // listSearch
            // 
            this.listSearch.AllowColumnReorder = true;
            this.listSearch.AllowDrop = true;
            this.listSearch.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.colNote,
            this.colCategory});
            resources.ApplyResources(this.listSearch, "listSearch");
            this.listSearch.FullRowSelect = true;
            this.listSearch.GridLines = true;
            this.helpProvider1.SetHelpKeyword(this.listSearch, resources.GetString("listSearch.HelpKeyword"));
            this.helpProvider1.SetHelpNavigator(this.listSearch, ((System.Windows.Forms.HelpNavigator)(resources.GetObject("listSearch.HelpNavigator"))));
            this.listSearch.MultiSelect = false;
            this.listSearch.Name = "listSearch";
            this.helpProvider1.SetShowHelp(this.listSearch, ((bool)(resources.GetObject("listSearch.ShowHelp"))));
            this.listSearch.SmallImageList = this.imgListTree;
            this.listSearch.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.toolTip.SetToolTip(this.listSearch, resources.GetString("listSearch.ToolTip"));
            this.listSearch.UseCompatibleStateImageBehavior = false;
            this.listSearch.View = System.Windows.Forms.View.Details;
            this.listSearch.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listSearch_ItemSelectionChanged);
            this.listSearch.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragEnter);
            this.listSearch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ArchiveForm_KeyDown);
            // 
            // colNote
            // 
            resources.ApplyResources(this.colNote, "colNote");
            // 
            // colCategory
            // 
            resources.ApplyResources(this.colCategory, "colCategory");
            // 
            // panel2
            // 
            resources.ApplyResources(this.panel2, "panel2");
            this.panel2.Controls.Add(this.picSearch);
            this.panel2.Controls.Add(this.txtSearch);
            this.panel2.Name = "panel2";
            this.helpProvider1.SetShowHelp(this.panel2, ((bool)(resources.GetObject("panel2.ShowHelp"))));
            // 
            // picSearch
            // 
            resources.ApplyResources(this.picSearch, "picSearch");
            this.helpProvider1.SetHelpKeyword(this.picSearch, resources.GetString("picSearch.HelpKeyword"));
            this.helpProvider1.SetHelpNavigator(this.picSearch, ((System.Windows.Forms.HelpNavigator)(resources.GetObject("picSearch.HelpNavigator"))));
            this.picSearch.Name = "picSearch";
            this.helpProvider1.SetShowHelp(this.picSearch, ((bool)(resources.GetObject("picSearch.ShowHelp"))));
            this.picSearch.TabStop = false;
            this.toolTip.SetToolTip(this.picSearch, resources.GetString("picSearch.ToolTip"));
            this.picSearch.Click += new System.EventHandler(this.picSearch_Click);
            // 
            // txtSearch
            // 
            this.txtSearch.AllowDrop = true;
            resources.ApplyResources(this.txtSearch, "txtSearch");
            this.txtSearch.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.helpProvider1.SetHelpKeyword(this.txtSearch, resources.GetString("txtSearch.HelpKeyword"));
            this.helpProvider1.SetHelpNavigator(this.txtSearch, ((System.Windows.Forms.HelpNavigator)(resources.GetObject("txtSearch.HelpNavigator"))));
            this.txtSearch.Name = "txtSearch";
            this.helpProvider1.SetShowHelp(this.txtSearch, ((bool)(resources.GetObject("txtSearch.ShowHelp"))));
            this.toolTip.SetToolTip(this.txtSearch, resources.GetString("txtSearch.ToolTip"));
            this.txtSearch.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragEnter);
            this.txtSearch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);
            // 
            // ctxMenuCategory
            // 
            this.ctxMenuCategory.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newCategoryStripMenuItem,
            this.removeStripMenuItem,
            this.toolStripSeparator7,
            this.printPreviewStripMenuItem,
            this.printCategoryStripMenuItem});
            this.ctxMenuCategory.Name = "ctxMenuCategory";
            this.helpProvider1.SetShowHelp(this.ctxMenuCategory, ((bool)(resources.GetObject("ctxMenuCategory.ShowHelp"))));
            resources.ApplyResources(this.ctxMenuCategory, "ctxMenuCategory");
            // 
            // newCategoryStripMenuItem
            // 
            this.newCategoryStripMenuItem.Name = "newCategoryStripMenuItem";
            resources.ApplyResources(this.newCategoryStripMenuItem, "newCategoryStripMenuItem");
            this.newCategoryStripMenuItem.Click += new System.EventHandler(this.menuNewCategory_Click);
            // 
            // removeStripMenuItem
            // 
            resources.ApplyResources(this.removeStripMenuItem, "removeStripMenuItem");
            this.removeStripMenuItem.Name = "removeStripMenuItem";
            this.removeStripMenuItem.Click += new System.EventHandler(this.menuRemoveCategory_Click);
            // 
            // toolStripSeparator7
            // 
            this.toolStripSeparator7.Name = "toolStripSeparator7";
            resources.ApplyResources(this.toolStripSeparator7, "toolStripSeparator7");
            // 
            // printPreviewStripMenuItem
            // 
            this.printPreviewStripMenuItem.Name = "printPreviewStripMenuItem";
            resources.ApplyResources(this.printPreviewStripMenuItem, "printPreviewStripMenuItem");
            this.printPreviewStripMenuItem.Click += new System.EventHandler(this.menuPrintPreview_Click);
            // 
            // printCategoryStripMenuItem
            // 
            resources.ApplyResources(this.printCategoryStripMenuItem, "printCategoryStripMenuItem");
            this.printCategoryStripMenuItem.Name = "printCategoryStripMenuItem";
            this.printCategoryStripMenuItem.Click += new System.EventHandler(this.menuPrintCategory_Click);
            // 
            // ctxMenuTreeNote
            // 
            this.ctxMenuTreeNote.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.moveToDesktopStripMenuItem,
            this.copyToDesktopStripMenuItem,
            this.toolStripSeparator8,
            this.removeNoteStripMenuItem,
            this.toolStripSeparator9,
            this.printToolStripMenuItem});
            this.ctxMenuTreeNote.Name = "ctxMenuTreeNote";
            this.helpProvider1.SetShowHelp(this.ctxMenuTreeNote, ((bool)(resources.GetObject("ctxMenuTreeNote.ShowHelp"))));
            resources.ApplyResources(this.ctxMenuTreeNote, "ctxMenuTreeNote");
            // 
            // moveToDesktopStripMenuItem
            // 
            this.moveToDesktopStripMenuItem.Name = "moveToDesktopStripMenuItem";
            resources.ApplyResources(this.moveToDesktopStripMenuItem, "moveToDesktopStripMenuItem");
            this.moveToDesktopStripMenuItem.Click += new System.EventHandler(this.MenuMoveToDesktop);
            // 
            // copyToDesktopStripMenuItem
            // 
            this.copyToDesktopStripMenuItem.Name = "copyToDesktopStripMenuItem";
            resources.ApplyResources(this.copyToDesktopStripMenuItem, "copyToDesktopStripMenuItem");
            this.copyToDesktopStripMenuItem.Click += new System.EventHandler(this.menuCopyToDesktop_Click);
            // 
            // toolStripSeparator8
            // 
            this.toolStripSeparator8.Name = "toolStripSeparator8";
            resources.ApplyResources(this.toolStripSeparator8, "toolStripSeparator8");
            // 
            // removeNoteStripMenuItem
            // 
            resources.ApplyResources(this.removeNoteStripMenuItem, "removeNoteStripMenuItem");
            this.removeNoteStripMenuItem.Name = "removeNoteStripMenuItem";
            this.removeNoteStripMenuItem.Click += new System.EventHandler(this.menuRemoveNote_Click);
            // 
            // toolStripSeparator9
            // 
            this.toolStripSeparator9.Name = "toolStripSeparator9";
            resources.ApplyResources(this.toolStripSeparator9, "toolStripSeparator9");
            // 
            // printToolStripMenuItem
            // 
            resources.ApplyResources(this.printToolStripMenuItem, "printToolStripMenuItem");
            this.printToolStripMenuItem.Name = "printToolStripMenuItem";
            this.printToolStripMenuItem.Click += new System.EventHandler(this.printStripMenuItem_Click);
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.archiveToolStripMenuItem});
            resources.ApplyResources(this.menuStrip1, "menuStrip1");
            this.menuStrip1.Name = "menuStrip1";
            this.helpProvider1.SetShowHelp(this.menuStrip1, ((bool)(resources.GetObject("menuStrip1.ShowHelp"))));
            // 
            // archiveToolStripMenuItem
            // 
            this.archiveToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuSave,
            this.toolStripSeparator10,
            this.mnuExit});
            this.archiveToolStripMenuItem.Name = "archiveToolStripMenuItem";
            resources.ApplyResources(this.archiveToolStripMenuItem, "archiveToolStripMenuItem");
            // 
            // mnuSave
            // 
            this.mnuSave.Image = global::DeskNotes.Properties.Resources.Save;
            this.mnuSave.Name = "mnuSave";
            resources.ApplyResources(this.mnuSave, "mnuSave");
            this.mnuSave.Click += new System.EventHandler(this.mnuSave_Click);
            // 
            // toolStripSeparator10
            // 
            this.toolStripSeparator10.Name = "toolStripSeparator10";
            resources.ApplyResources(this.toolStripSeparator10, "toolStripSeparator10");
            // 
            // mnuExit
            // 
            this.mnuExit.Name = "mnuExit";
            resources.ApplyResources(this.mnuExit, "mnuExit");
            this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
            // 
            // fontDialog
            // 
            this.fontDialog.FontMustExist = true;
            this.fontDialog.ShowColor = true;
            // 
            // helpProvider1
            // 
            resources.ApplyResources(this.helpProvider1, "helpProvider1");
            // 
            // ArchiveForm
            // 
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.menuStrip1);
            this.helpProvider1.SetHelpKeyword(this, resources.GetString("$this.HelpKeyword"));
            this.helpProvider1.SetHelpNavigator(this, ((System.Windows.Forms.HelpNavigator)(resources.GetObject("$this.HelpNavigator"))));
            this.Name = "ArchiveForm";
            this.helpProvider1.SetShowHelp(this, ((bool)(resources.GetObject("$this.ShowHelp"))));
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ArchiveForm_FormClosing);
            this.Load += new System.EventHandler(this.ArchiveForm_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ArchiveForm_KeyDown);
            this.toolStripContainer1.BottomToolStripPanel.ResumeLayout(false);
            this.toolStripContainer1.BottomToolStripPanel.PerformLayout();
            this.toolStripContainer1.ContentPanel.ResumeLayout(false);
            this.toolStripContainer1.TopToolStripPanel.ResumeLayout(false);
            this.toolStripContainer1.TopToolStripPanel.PerformLayout();
            this.toolStripContainer1.ResumeLayout(false);
            this.toolStripContainer1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.ctxMenuArchNote.ResumeLayout(false);
            this.toolStripRTF.ResumeLayout(false);
            this.toolStripRTF.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.tabControl1.ResumeLayout(false);
            this.tabArchive.ResumeLayout(false);
            this.tabSearch.ResumeLayout(false);
            this.tabSearch.PerformLayout();
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.picSearch)).EndInit();
            this.ctxMenuCategory.ResumeLayout(false);
            this.ctxMenuTreeNote.ResumeLayout(false);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

        /// <summary>
        /// Text assigned to note displayed on DeskNote RichTextBox
        /// Set - plain text or RTF text
        /// Get - plain text only
        /// </summary>
        internal string TextEx
        {
            set
            {
                if (value.StartsWith(RtfConverter.RTF_TAG))
                    txtNote.Rtf = value;
                else
                    txtNote.Text = value;
            }
        }

		private void treeArchive_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			CategoryNode cn = (CategoryNode) e.Node;
            cn.SetImage();
			deskNotes.UpdateCategoryExpanded(cn.ID, cn.IsExpanded);
            NeedSave();
		}

		private void treeArchive_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			CategoryNode cn = (CategoryNode) e.Node;
            cn.SetImage();
			deskNotes.UpdateCategoryExpanded(cn.ID, cn.IsExpanded);
            NeedSave();
		}

        private void treeArchive_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (searchListUsed)
            {
                e.Cancel = true;
            }

        }

        /// <summary>
        /// Save Archived notes and Searchable Index when form is closing.
        /// Remember position, size and selected node.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void ArchiveForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.UpdateChangedNote();
			RememberSelectedNode(treeArchive.SelectedNode);
            Rectangle r = new Rectangle(this.Left, this.Top, this.Width, this.Height);
            Properties.Settings.Default.ArchivePosition = r;
            Properties.Settings.Default.ArchiveSplitter = splitContainer1.SplitterDistance;
            if (deskNotes.archiveModified)
            { //Dialog is closing because of e.g. Windows shutdown and it was modified
                // save archive anyway
                deskNotes.SaveArchive();
                index.Save();
                index.Dispose();
            }
            /*
            if ((deskNotes.archiveModified) && ((e.CloseReason == CloseReason.UserClosing) || (e.CloseReason==CloseReason.ApplicationExitCall))){
                DialogResult res = MessageBox.Show(this, MainResource.GetString("ArchiveNotSavedClosing"),MainResource.GetString("APP_TITLE"),
                    MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
                if (res == DialogResult.Yes){
        			deskNotes.SaveArchive();
                    index.Save();
                   } else if (res == DialogResult.Cancel){
                       e.Cancel = true;
                   }
               } else if (deskNotes.archiveModified) { //Dialog is closing because of e.g. Windows shutdown and it was modified
                   // save archive anyway
                   deskNotes.SaveArchive();
                   index.Save();
               }*/
        }

		/// <summary>
		/// After node is selected in the tree appropriate text must be displayed in Note's rich text box.
		/// For this time rich text box TextChanged event handler is disabled.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			txtNote.TextChanged -= new EventHandler(this.Note_TextChanged);
			currentNoteChanged = false;
			if (e.Node is ArchivedNoteNode)
			{
                Properties.Settings settings = Properties.Settings.Default;

				ArchivedNoteNode archNote = (ArchivedNoteNode) e.Node;
				//this.txtNote.Rtf = archNote.NoteText;
				this.lblArchivedTime.Text = archNote.ArchivedTimeText;
                this.lblModificationTime.Text = archNote.LastModificationTimeText;
               
                txtNote.ForeColor = archNote.PanelTextColor;
                txtNote.BackColor = archNote.PanelBackColor;
                txtNote.Font = null != archNote.Font ? archNote.Font : settings.Font;
            
                this.TextEx = archNote.NoteText;
			} 
			else 
			{
				this.txtNote.BackColor = this.backColor;
				this.txtNote.Text = "";
				this.lblArchivedTime.Text = "";
			}
			DeskNotesTreeNode node = (DeskNotesTreeNode) e.Node;
			//Show or hide edited picture according to node's state
			if (node.Edited)
				btnSave.Enabled = true;
			else
                btnSave.Enabled = false;

			txtNote.TextChanged += new EventHandler(this.Note_TextChanged);
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Hide();
		}

		/// <summary>
		/// Displays context menu based on underlying node.
		/// Category and Note have own context menus.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			base.OnMouseUp(e);
			
			if (e.Button == MouseButtons.Right)
			{
				TreeNode node = treeArchive.GetNodeAt(e.X, e.Y);
				if (null == node) //When user clicks outside of node then do nothing.
					return;

				treeArchive.SelectedNode = node;
				
				if (node is ArchivedNoteNode)
				{
					ctxMenuTreeNote.Show(treeArchive, new Point(e.X, e.Y));
				} 
				else 
				{
					if (null == node.Parent)
						//disable Remove menu item for root node
						removeStripMenuItem.Enabled = false;
					else
                        removeStripMenuItem.Enabled = true;
					ctxMenuCategory.Show(treeArchive, new Point(e.X, e.Y));
				}
			}
		}

	

		/// <summary>
		/// Only Category label can be edited
		/// Notes label cannot and action is cancelled at this case.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_BeforeLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			if(e.Node is ArchivedNoteNode)
			{
				e.CancelEdit = true;
			}
		}

		/// <summary>
		/// When Category label was edited then Archive dataset is updated with changes
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			if ((e.Node is CategoryNode) && (null != e.Label))
			{
				CategoryNode cat = (CategoryNode) e.Node;
				deskNotes.UpdateCategory(cat.ID, cat.Parent, e.Label, cat.IsExpanded);
				NeedSave();
				cat.Edited = true;
                cat.SetImage();
			}
		}

		/// <summary>
		/// When Note's text is changed for a very first time ie first key is pressed 
		/// then status of Note's node is changed and Edited flag is set.
		/// This event handler is called for every char pressed!
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Note_TextChanged(object sender, System.EventArgs e)
		{
            // Workaround! When search terms in txtNote have been unmarked TextChanged event 
            // is still raised for these changes even it was disabled "-=" before this process.
            // To avoid note to be marked as modified use this state value.
            if (ignoreModificationAfterMarks)
            {
                ignoreModificationAfterMarks = false;
                return;
            }

			if (currentNoteChanged == false)
			{
				currentNoteChanged = true;
				NeedSave();
				ArchivedNoteNode archNote = (ArchivedNoteNode)treeArchive.SelectedNode;
				archNote.Edited = true;
			}
		}
        private void mnuSave_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            deskNotes.Exit();
        }

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			try
            {
                UpdateChangedNote();
                deskNotes.SaveArchive();
                if (null != index)
                    index.Save();
			} 
			catch (System.Security.SecurityException se)
			{
				string text = string.Format(MainResource.GetString("ERROR_WRITE_DATA"), Properties.Settings.Default.ArchiveFile, se.Message + "\n");
				if ( DialogResult.No == MessageBox.Show(this, text, MainResource.GetString("APP_TITLE"),
				MessageBoxButtons.YesNo, MessageBoxIcon.Error))
				return;
			}
			this.ArchiveSaved();
		}

		/// <summary>
		/// When Note's text area is being left then changed text is written
		/// into Archive dataset.
		/// !!! There can be a state when another app's window is selected
		/// then Leave event does not happen. Or form is made hidden,...
		/// These states must be handled elsewhere !!!!
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void txtNote_Leave(object sender, System.EventArgs e)
		{
			UpdateChangedNote();
		}

		private void ArchiveForm_Deactivate(object sender, System.EventArgs e)
		{
			//Logger.WriteLine("Archive deactivated");
			UpdateChangedNote();
		}

        private void treeArchive_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            UpdateChangedNote();
        }
		/// <summary>
		/// Updates changed note when text or form lost its focus.
		/// </summary>
		private void UpdateChangedNote()
		{
			if (treeArchive.SelectedNode is ArchivedNoteNode)
			{
				ArchivedNoteNode node = (ArchivedNoteNode) treeArchive.SelectedNode;
				//if Note's node was CURRENTLY edited then update Arch. dataset.
				if ((node.Edited) && (currentNoteChanged))
				{
                    node.LastModificationTime = DateTime.Now;
					node.NoteText = txtNote.Rtf;
					deskNotes.UpdateArchivedNote(node, ((CategoryNode)node.Parent).ID);
                    index.Update(node.NoteText, node.ID);
					currentNoteChanged = false;
				}
			}
		}

		/// <summary>
		/// Saves ID of selected node into registry.
		/// </summary>
		/// <param name="selectedNode">Node selected in archive tree.</param>
		private void RememberSelectedNode(TreeNode selectedNode)
		{
			DeskNotesTreeNode node = (DeskNotesTreeNode) selectedNode;
			if (null != node)
			{
				Properties.Settings.Default.SelectedNodeId = node.ID;
				Properties.Settings.Default.SelectedNodeType = node.Type;
                Properties.Settings.Default.Save();
			}
		}

	
		/// <summary>
		/// Creates archived node for given note under given category
		/// </summary>
		/// <param name="category">Category under which note is archived</param>
		/// <param name="note">DeskNotes Note to be archived.</param>
		/// <returns></returns>
		private ArchivedNoteNode DoArchiving(CategoryNode category, Note note)
		{
			ArchivedNoteNode archNode = new ArchivedNoteNode(note); 
			category.Nodes.Add(archNode);
			deskNotes.ArchiveDeskNote(archNode, note);
			// Remove desknote from desktop and from dataset
			note.RemoveRecord();
			note.MyForm.Close();
			//treeArchive.SelectedNode = archNode;
			return archNode;
		}

		/// <summary>
		/// Archive into selected or default tree category.
		/// </summary>
		/// <param name="note">DeskNotes note to be archived directly</param>
		/// <returns></returns>
		internal bool Archive(Note note)
		{
			CategoryNode category;
			TreeNode node = treeArchive.SelectedNode;
			if (null == node) //set root node
				node = (TreeNode)treeArchive.Nodes[0];
			
			if (node is ArchivedNoteNode)
				category = (CategoryNode) node.Parent;
			else
				category = (CategoryNode) node;

			ArchivedNoteNode archNode = DoArchiving(category, note);
			if (null != archNode)
			{
				archNode.Edited = true;
				NeedSave();
                treeArchive.SelectedNode = archNode;
                if (null != index)
                    index.Add(archNode.NoteText, archNode.ID);
				return true;
			}
			else
				return false;
		}

		/// <summary>
		/// Handles actual drop of data into tree view. Used by drag&drop during archiving and
		/// move of archived nodes too.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			ArchivedNoteNode archNode = null;
			CategoryNode category;

			TreeNode node = treeArchive.GetNodeAt(treeArchive.PointToClient(new Point(e.X, e.Y)));
			
			//if there is no node under cursor then use selected one or root node.
			if (null == node)
			{
				node = (TreeNode)treeArchive.SelectedNode;
				if (null == node) //set root node
					node = (TreeNode)treeArchive.Nodes.GetEnumerator().Current;
			}

			if (node is ArchivedNoteNode)
				category = (CategoryNode) node.Parent;
			else
				category = (CategoryNode) node;

			if (e.Data.GetDataPresent((new Note()).GetType()))
			{ //Archivation of desknote from desktop
				Note note = (Note)e.Data.GetData((new Note()).GetType());
				archNode = DoArchiving(category, note);
			} 
			else if (e.Data.GetDataPresent( (new CategoryNode()).GetType()))
			{ // Move whole category to another one
				CategoryNode movedCat = (CategoryNode) e.Data.GetData((new CategoryNode()).GetType());
				deskNotes.UpdateCategory(movedCat.ID, category, movedCat.Text, movedCat.IsExpanded);
				movedCat.Parent.Nodes.Remove(movedCat);
				category.Nodes.Add(movedCat);
				movedCat.Edited = true;
				NeedSave();
			} 
			else if (e.Data.GetDataPresent( (new ArchivedNoteNode()).GetType()))
			{ // Move one archived note under different category
				archNode = (ArchivedNoteNode) e.Data.GetData((new ArchivedNoteNode()).GetType());
				//CategoryNode parent = (CategoryNode) archNode.Parent;
				deskNotes.UpdateArchivedNote(archNode, category.ID);
				archNode.Parent.Nodes.Remove(archNode);
				category.Nodes.Add(archNode);
			}
			//if drop was really done then assign node and mark it edited and required to be saved.
			if (archNode != null)
			{
				archNode.Edited = true;
				NeedSave();
				treeArchive.SelectedNode = archNode;
				txtNote.DetectUrls = false;
				txtNote.DetectUrls = true;
			}
		    
		}

		private void treeArchive_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
            if ("tabSearch" == tabControl1.SelectedTab.Name)
            {
                // switch to Tree view tab to allow drop of archived note
                tabControl1.SelectTab(0);
            }
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else if ((e.Data.GetDataPresent((new Note()).GetType())) ||
                    (e.Data.GetDataPresent((new ArchivedNoteNode()).GetType())) ||
                    (e.Data.GetDataPresent((new CategoryNode()).GetType())))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
		}

		/// <summary>
		/// Node is expanded if data being dragged are correct and node under mouse is category.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if ( (e.Data.GetDataPresent(DataFormats.Text)) ||
				(e.Data.GetDataPresent((new Note()).GetType())) ) 
			{
				DeskNotesTreeNode target = (DeskNotesTreeNode) treeArchive.GetNodeAt(treeArchive.PointToClient(new Point(e.X, e.Y)));
				if (target is CategoryNode)
				{
					target.Expand();
				}
			}
		}

		/// <summary>
		/// Remembers selected node. The node is then used when drag&drop inside tree starts.
		/// ItemDrag method does not change selection of node if node is not clicked before.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			TreeNode node = treeArchive.GetNodeAt(e.X, e.Y);
			if (null != node) //only when node was under the mouse when clicked
				selectedNode = node;
		}

		/// <summary>
		/// Moves nodes between categories in the archive form tree view. 
		/// Does not move root node
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
		{
			treeArchive.SelectedNode = selectedNode;			
			if (selectedNode is ArchivedNoteNode)
				this.DoDragDrop((ArchivedNoteNode)selectedNode, DragDropEffects.Move);
			else if (((CategoryNode)selectedNode).Parent != null)
				this.DoDragDrop((CategoryNode)selectedNode, DragDropEffects.Move);
		}

		/// <summary>
		/// Moves selected Note back to desktop, removes it from archive data 
		/// and saves the archive.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MenuMoveToDesktop(object sender, System.EventArgs e)
		{
			ArchivedNoteNode node = (ArchivedNoteNode)treeArchive.SelectedNode;
			Note tempNote = deskNotes.GetNoteFromArchive(node.ID);
			if (deskNotes.ShowArchivedNote(tempNote))
			{
				if (deskNotes.RemoveArchivedNote(node.ID))
				{
					DeskNotesTreeNode parent = (DeskNotesTreeNode)node.Parent;
					parent.Edited = true;
					treeArchive.Nodes.Remove(node);
					this.NeedSave();
					treeArchive.SelectedNode = parent;
                    RemoveFromSearchResults(node.ID);
				}
			}
		}

		/// <summary>
		/// Removes archived note from archive
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuRemoveNote_Click(object sender, System.EventArgs e)
		{
			ArchivedNoteNode node = (ArchivedNoteNode)treeArchive.SelectedNode;
			if (deskNotes.RemoveArchivedNote(node.ID))
			{
                index.Remove(node.ID);
				DeskNotesTreeNode parent = (DeskNotesTreeNode) node.Parent;
				parent.Edited = true;
				this.NeedSave();
				treeArchive.Nodes.Remove(node);
				treeArchive.SelectedNode = parent;
                RemoveFromSearchResults(node.ID);
			}
		}

        /// <summary>
        /// Removes ArchiveNodeNote from SearchItems collection by ID, if note is in search result list.
        /// </summary>
        /// <param name="id">ID of ArchivedNodeNote being removed</param>
        private void RemoveFromSearchResults(long id)
        {
            SearchResultItem i = FindSearchResultItem(id);
            if (null != i)
            {
                i.TreeNode = null;
                listSearch.Items.Remove(i);
            }
        }

        private SearchResultItem FindSearchResultItem(long id)
        {
            if (listSearch.Items.Count > 0)
            {
                foreach (ListViewItem i in listSearch.Items)
                {
                    if (((SearchResultItem)i).ID == id)
                    {
                        return (SearchResultItem)i;
                    }
                }
            }
            return null;
        }

		/// <summary>
		/// Adds new category with default text under currently selected category.
		/// Marks new category as changed and makes it editable.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuNewCategory_Click(object sender, System.EventArgs e)
		{
			CategoryNode parent = (CategoryNode) treeArchive.SelectedNode;
			CategoryNode node = new CategoryNode(MainResource.GetString("DEFAULT_ARCHIVE_CATEGORY"));
			parent.Nodes.Add(node);
			deskNotes.AddArchiveCategory(node);
			this.NeedSave();
			node.Edited = true;
			treeArchive.SelectedNode = node;
			node.BeginEdit();
		}


		/// <summary>
		/// Removes selected category from tree and archive with all its
		/// subcategories and nodes.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuRemoveCategory_Click(object sender, System.EventArgs e)
		{
			CategoryNode node = (CategoryNode) treeArchive.SelectedNode;
			if (DialogResult.Yes == MessageBox.Show(this, MainResource.GetString("CONFIRM_CATEGORY_REMOVE"),
				MainResource.GetString("APP_TITLE"),MessageBoxButtons.YesNo,MessageBoxIcon.Question))
			{
				if (deskNotes.RemoveCategory(node.ID))
				{
					CategoryNode parent = (CategoryNode)node.Parent;
					parent.Edited = true;
					treeArchive.SelectedNode = parent;
					node.Remove();
					this.NeedSave();
				}
			}
		}

        // TODO - Copy to Desktop fails with exception for Archived time for Note archived in older version.

		/// <summary>
		/// Makes a copy of desknote on desktop.
		/// Archived one remains in the archive.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuCopyToDesktop_Click(object sender, System.EventArgs e)
		{
			ArchivedNoteNode node = (ArchivedNoteNode)treeArchive.SelectedNode;
			Note tempNote = deskNotes.GetNoteFromArchive(node.ID);
			deskNotes.ShowArchivedNote(tempNote);
		}

		private void insertTimeStripMenuItem_Click(object sender, System.EventArgs e)
		{
            System.Text.StringBuilder timeStamp = new System.Text.StringBuilder(deskNotes.TimeStamp);
            timeStamp.Insert(0, "\n");
            timeStamp.Append("\n");
            txtNote.SelectedText = timeStamp.ToString();
		}

		private void txtNote_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
		{
			try
			{
				System.Diagnostics.Process.Start(e.LinkText);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, MainResource.GetString("CANNOT_OPEN_LINK") + ex.Message, MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
			}
		}

		private void printStripMenuItem_Click(object sender, System.EventArgs e)
		{
			try 
			{
				ArchivedNoteNode node = (ArchivedNoteNode) treeArchive.SelectedNode;
				deskNotes.PrintOneNote(node, null);
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"),ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void menuPrintPreview_Click(object sender, System.EventArgs e)
		{
			try 
			{
				ArrayList list = new ArrayList(treeArchive.SelectedNode.Nodes.Count);
				GetSubNodes(treeArchive.SelectedNode, ref list);
				ArchivedNoteNode[] notes = new ArchivedNoteNode[list.Count];
				list.CopyTo(notes);
                CoolPrintPreview.CoolPrintPreviewDialog printPreview = new CoolPrintPreview.CoolPrintPreviewDialog(deskNotes, notes);
				printPreview.Show();
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"), ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// Recursively fills children array with ArchivedNoteNodes
		/// </summary>
		/// <param name="node">Node to start from, should be category</param>
		/// <param name="children">List of ArchivedNoteNodes under given category and all its subcategories.</param>
		internal void GetSubNodes(TreeNode node, ref ArrayList children)
		{
			foreach (TreeNode child in node.Nodes)
			{
				if (child is ArchivedNoteNode)
					children.Add(child);
				else
					GetSubNodes( child, ref children );
			}
		}

		/// <summary>
		/// Prints all notes under given category
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuPrintCategory_Click(object sender, System.EventArgs e)
		{
			try 
			{
				ArrayList list = new ArrayList(treeArchive.SelectedNode.Nodes.Count);
				GetSubNodes(treeArchive.SelectedNode, ref list);
				ArchivedNoteNode[] notes = new ArchivedNoteNode[list.Count];
				list.CopyTo(notes);
                deskNotes.PrintNotes(notes, null); //(MenuItem) sender, this.menuPrint, this.menuPrint2);
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"), ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		
		}

        /// <summary>
        /// Change Bullet style for current selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBullet_Click(object sender, EventArgs e)
        {
            txtNote.SelectionBullet = !txtNote.SelectionBullet;
            if (btnBullet.Checked)
                btnNumbered.Checked = false;
            Note_TextChanged(null, null);

        }
        private void btnNumbered_Click(object sender, EventArgs e)
        {
            short listType = RtfFormats.GetListType(txtNote);
            short currentListType = RtfFormats.NumberedBulletList(txtNote, !RtfFormats.SelectionNumbered(listType), (short)WinImports.AdvRichTextBulletType.Number);
            if (currentListType != (short)WinImports.AdvRichTextBulletType.NoList)
            {
                btnBullet.Checked = false;
                btnNumbered.Checked = true;
            }
            else
            {
                btnNumbered.Checked = false;
            }
            Note_TextChanged(null, null);

        }

        private void txtNote_SelectionChanged(object sender, EventArgs e)
        {
            //btnBullet.Checked = txtNote.SelectionBullet;
            CheckToolbarButtons(RtfFormats.GetListType(txtNote));
            if (null != txtNote.SelectionFont)
            {
                btnBold.Checked = txtNote.SelectionFont.Bold;
                btnItalic.Checked = txtNote.SelectionFont.Italic;
                btnStrike.Checked = txtNote.SelectionFont.Strikeout;
                btnUnderline.Checked = txtNote.SelectionFont.Underline;
            }
        }

        /// <summary>
        /// Unmark text in note which was marked for searched terms after search query.
        /// Original Rtf text is restored from saved values.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNote_Enter(object sender, EventArgs e)
        {
            if (searchMarksOn)
            {
                searchMarksOn = false;
                
                if (treeArchive.SelectedNode is ArchivedNoteNode)
                {
                    ArchivedNoteNode note = (ArchivedNoteNode)treeArchive.SelectedNode;
                    SearchResultItem result = FindSearchResultItem(note.ID);
                    if (result != null)
                    {
                        // !!! Disabling TextChanged event does not have an effect here !!!!
                        txtNote.TextChanged -= this.Note_TextChanged;
                        foreach (SearchedText st in result.SearchedText)
                        {
                            txtNote.Select(st.selectionStart, st.length);
                            txtNote.SelectedRtf = st.selectedRtf;
                        }
                        ignoreModificationAfterMarks = true;
                        txtNote.TextChanged += this.Note_TextChanged;
                    }
                }
            }
        }

        /// <summary>
        /// Set toolbar buttons checked/unchecked state for bullet/numbered lists
        /// </summary>
        private void CheckToolbarButtons(short listType)
        {
            if (toolStripRTF.Visible)
            {
                //short listType = GetListType();
                switch ((WinImports.AdvRichTextBulletType)listType)
                {
                    case WinImports.AdvRichTextBulletType.NoList:
                        //no button toggled
                        btnBullet.Checked = false;
                        btnNumbered.Checked = false;
                        break;
                    case WinImports.AdvRichTextBulletType.Normal:
                        btnBullet.Checked = true;
                        btnNumbered.Checked = false;
                        break;
                    default:
                        // only numbered lists
                        btnBullet.Checked = false;
                        btnNumbered.Checked = true;
                        break;
                }
            }
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Bold)
                newFont = new Font(f, f.Style ^ FontStyle.Bold);
            else
                newFont = new Font(f, f.Style | FontStyle.Bold);
            txtNote.SelectionFont = newFont;
            btnBold.Checked = newFont.Bold;
            Note_TextChanged(null, null);
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Italic)
                newFont = new Font(f, f.Style ^ FontStyle.Italic);
            else
                newFont = new Font(f, f.Style | FontStyle.Italic);
            txtNote.SelectionFont = newFont;
            btnItalic.Checked = newFont.Italic;
            Note_TextChanged(null, null);
        }

        private void btnStrike_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Strikeout)
                newFont = new Font(f, f.Style ^ FontStyle.Strikeout);
            else
                newFont = new Font(f, f.Style | FontStyle.Strikeout);
            txtNote.SelectionFont = newFont;
            Note_TextChanged(null, null);
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Underline)
                newFont = new Font(f, f.Style ^ FontStyle.Underline);
            else
                newFont = new Font(f, f.Style | FontStyle.Underline);
            txtNote.SelectionFont = newFont;
            btnUnderline.Checked = newFont.Underline;
            Note_TextChanged(null, null);
        }

        private void btnIndentRight_Click(object sender, EventArgs e)
        {
            txtNote.SelectionIndent += 5;
            Note_TextChanged(null, null);
        }

        private void btnIndentLeft_Click(object sender, EventArgs e)
        {
            if (txtNote.SelectionIndent >= 5)
            {
                txtNote.SelectionIndent -= 5;
                Note_TextChanged(null, null);
            }
        }

        /// <summary>
        /// Opens font dialog for current selection font.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFont_Click(object sender, EventArgs e)
        {
            fontDialog.Font = txtNote.SelectionFont;
            fontDialog.Color = txtNote.SelectionColor;
            if (DialogResult.OK == fontDialog.ShowDialog())
            {
                txtNote.SelectionFont = fontDialog.Font;
                txtNote.SelectionColor = fontDialog.Color;
                Note_TextChanged(null, null);
            }
        }

        private void btnSelectionBackColor_Click(object sender, EventArgs e)
        {
            colorDialog.Color = txtNote.BackColor;
            if (DialogResult.OK == colorDialog.ShowDialog())
            {
                txtNote.SelectionBackColor = colorDialog.Color;
                if (txtNote.SelectionLength >0)
                    Note_TextChanged(null, null);
            }
        }
        private void btnNoteBackColor_Click(object sender, EventArgs e)
        {
            colorDialog.Color = txtNote.BackColor;
            if (DialogResult.OK == colorDialog.ShowDialog())
            {
                txtNote.BackColor = colorDialog.Color;
                ArchivedNoteNode archNote = (ArchivedNoteNode)treeArchive.SelectedNode;
                archNote.PanelBackColor = colorDialog.Color;
            }
            Note_TextChanged(null, null);
        }

        private void toolbarStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.toolbarStripMenuItem.Checked)
                this.toolStripRTF.Show(); 
            else
                this.toolStripRTF.Hide();
        }

        private void statusbarStripMenuItem_Click(object sender, EventArgs e)
        {
            if (statusbarStripMenuItem.Checked)
                this.statusStrip1.Show();
            else
                this.statusStrip1.Hide();
        }

        private void ctxMenuArchNote_Opening(object sender, CancelEventArgs e)
        {
            statusbarStripMenuItem.Checked = statusStrip1.Visible;
            toolbarStripMenuItem.Checked = toolStripRTF.Visible;
        }

        private void txtNote_MouseDown(object sender, MouseEventArgs e)
        {
            if (txtNote.SelectionLength > 0)
            {
                Size dragSize = SystemInformation.DragSize;
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                    e.Y - (dragSize.Height / 2)), dragSize);
            }
        }

        private void txtNote_MouseUp(object sender, MouseEventArgs e)
        {
            dragBoxFromMouseDown = Rectangle.Empty;
        }

        private void txtNote_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Left) &&
                    (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y)))
            {
                // Proceed with the drag-and-drop, passing in the text in.    
                DataObject dO = new DataObject(DataFormats.Rtf, txtNote.SelectedRtf);
                // Copy is on Windows normal only when user presses Ctrl as well, Move
                // is performed when no key is pressed in addition to mouse. But RTFBox allows only Copy
                // operation with no key presses in addition to other application, but Ctrl for itself.
                DragDropEffects dropEffect = txtNote.DoDragDrop(dO, DragDropEffects.Copy);
                dragBoxFromMouseDown = Rectangle.Empty;
            }
        }

        void txtNote_DragDrop(object sender, DragEventArgs e)
        {
            //Logger.WriteLine("DragDrop started");

            int i;
            String s;
            if (e.Data.GetDataPresent(DataFormats.Rtf))
            {
                txtNote.SelectedRtf = e.Data.GetData(DataFormats.Rtf).ToString();
            }
            else if (e.Data.GetDataPresent(DataFormats.Text))
            {
                // Get start position to drop the text.
                i = txtNote.SelectionStart;
                s = txtNote.Text.Substring(i);
                txtNote.Text = txtNote.Text.Substring(0, i);

                // Drop the text on to the RichTextBox.
                txtNote.Text = txtNote.Text +
                   e.Data.GetData(DataFormats.Text).ToString();
                txtNote.Text = txtNote.Text + s;
            }
        }

        void txtNote_DragEnter(object sender, DragEventArgs e)
        {
            if ((e.Data.GetDataPresent(DataFormats.Text)) || (e.Data.GetDataPresent(DataFormats.Rtf)))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;

            //Logger.WriteLine("DragEnter effect is:" + e.Effect);
        }

        private void txtNote_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.Shift))
            {
                switch (e.KeyCode)
                {
                    case Keys.F:
                        btnFont_Click(null, null);
                        break;
                    case Keys.B:
                        // Ctrl+Shift+B makes line bullet
                        this.btnBullet_Click(sender, null);
                        break;
                    case Keys.L:
                        //Ctrl+Shift+L  switches among various list types
                        short listType = RtfFormats.GetListTypeAfterMessage(RtfFormats.GetListType(txtNote));
                        short currentListType = RtfFormats.NumberedBulletList(txtNote, true, listType);
                        CheckToolbarButtons(listType);
                        e.SuppressKeyPress = true;
                        break;
                }
            } else if (e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.B:
                        this.btnBold_Click(sender, null);
                        break;
                    case Keys.I:
                        this.btnItalic_Click(sender, null);
                        // SuppressKeyPress has to be called because some unknown maybe 
                        // native control code interprets this as insert "\t" into text box.
                        e.SuppressKeyPress = true;
                        break;
                    case Keys.U:
                        this.btnUnderline_Click(sender, null);
                        break;
                    case Keys.S:
                        this.btnSave_Click(sender, null);
                        break;
                    case Keys.Oemcomma:
                        this.btnIndentLeft_Click(sender, null);
                        break;
                    case Keys.OemPeriod:
                        this.btnIndentRight_Click(sender, null);
                        break;
                    case Keys.N:
                        CreateNewNote();
                        break;
                    case Keys.Oemplus:
                    case Keys.Add:
                        e.SuppressKeyPress = true;
                        Font old = txtNote.SelectionFont;
                        float size = old.SizeInPoints + 2;
                        Font f = new Font(old.Name, size, old.Style);
                        txtNote.SelectionFont = f;
                        break;
                    case Keys.OemMinus:
                    case Keys.Subtract:
                        e.SuppressKeyPress = true;
                        Font old2 = txtNote.SelectionFont;
                        float size2 = old2.SizeInPoints - 2;
                        Font f2 = new Font(old2.Name, size2, old2.Style);
                        txtNote.SelectionFont = f2;
                        break;
                }
            }
        }

        /// <summary>
        /// Wrapper for unified creation of new DeskNote from Archive Form
        /// </summary>
        private void CreateNewNote()
        {
            Note n = deskNotes.CreateNewNote(MainResource.GetString("FIRST_NOTE"), Note.DefaultPosition);
            n.MyForm.Activate();
            n.MyForm.SetResizableBorder(); 
        }

      

        /// <summary>
        /// Searchable Index is being created from Archive, reports progress
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void index_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            lblIndexing.Visible = true;
            indexProgress.Visible = true;
            lblArch.Visible = false;
            lblArchivedTime.Visible = false;
            lblMod.Visible = false;
            lblModificationTime.Visible = false;
            indexProgress.Value = e.ProgressPercentage;
        }

        /// <summary>
        /// Searchable Index was completed from Archive. It is either done or was cancelled.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void index_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            this.Cursor = Cursors.Default;
            lblIndexing.Visible = false;
            indexProgress.Visible = false;
            lblArch.Visible = true;
            lblArchivedTime.Visible = true;
            lblMod.Visible = true;
            lblModificationTime.Visible = true;
        }

        /// <summary>
        /// Performs search in Searchable Index and fills list view
        /// </summary>
        internal void Search()
        {
            listSearch.Items.Clear();
            List<SearchedText> markText = new List<SearchedText>();
            try
            {
                SearchableArchiveDB.NotesRow[] result = index.Search(txtSearch.Text, ref markText);
                if (0 < result.Length)
                {
                    foreach (SearchableArchiveDB.NotesRow note in result)
                    {
                        SearchResultItem item = new SearchResultItem(note.text, note.ID, markText);
                        item.SubItems[1].Text = deskNotes.GetCategoryNameFromNoteID(note.ID);
                        listSearch.Items.Add(item);
                    }
                    listSearch.Focus();
                    listSearch.Items[0].Selected = true;
                }
                else
                { // no results found
                    listSearch.Items.Add(MainResource.GetString("NoSearchResultFound"));
                }
            } 
            catch (IndexOutOfRangeException ie)
            {
                listSearch.Items.Add(MainResource.GetString("NoSearchResultFound"));
                Logger.WriteLine("Exception: '{0}'. " + ie.StackTrace, ie.Message);
            }
        }

        /// <summary>
        /// Starts search from click on picture box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        private void picSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        /// <summary>
        /// Starts search when Enter is pressed in text field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter == e.KeyCode)
            {
                Search();
                // If SupressKeyPress is not called then this method BEEPS!
                e.SuppressKeyPress = true;
            }
            else if (e.Control && (e.KeyCode == Keys.N))
            {
                CreateNewNote();
                e.SuppressKeyPress = true;
            }

        }

        private void listSearch_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                if (e.Item is SearchResultItem)
                {
                    //treeArchive.AfterExpand -= this.treeArchive_AfterExpand;
                    //Logger.WriteLine("List selected ID: " + ((SearchResultItem)e.Item).ID.ToString());
                    this.Cursor = Cursors.WaitCursor;
                    SearchResultItem result = (SearchResultItem)e.Item;
                    ArchivedNoteNode note = result.TreeNode;
                    if (null == note)
                    {
                        note = LookUpArchivedNode(treeArchive.Nodes, result.ID);
                        result.TreeNode = note;
                    }

                    if (null != note)
                    {
                        searchListUsed = true;
                        treeArchive.SelectedNode = note;
                        MarkSearchedText(result);
                        searchListUsed = false;
                    }
                        // when NoteNode is null, e.g. it was removed then remove this item from the list
                        // Does not work as NoteNode is not disposed.
                        //listSearch.Items.Remove(result);
                    this.Cursor = Cursors.Default;
                    //treeArchive.AfterExpand -= this.treeArchive_AfterExpand;
                }
             }
        }

        private void MarkSearchedText(SearchResultItem result)
        {
            // Mark the searched text in Note
            // Unregister event handlers for text change and selection change
            txtNote.TextChanged -= this.Note_TextChanged;
            txtNote.SelectionChanged -= this.txtNote_SelectionChanged;
            try
            {
                foreach (SearchedText q in result.SearchedText)
                {
                    int position = txtNote.Find(q.searchTerm, RichTextBoxFinds.NoHighlight);
                    if (position != -1)
                    {
                        txtNote.Select(position, q.searchTerm.Length);
                        q.Selection(position, q.searchTerm.Length, txtNote.SelectedRtf);
                        txtNote.SelectionBackColor = Color.LightBlue;
                        txtNote.SelectionColor = Color.White;
                        searchMarksOn = true;
                    }
                }
                txtNote.SelectionStart = 0;
                txtNote.SelectionLength = 0;
            }
            catch (Exception ex) {
                Logger.WriteLine(ex.Message + "\\n" + ex.StackTrace);
            }
            finally
            {
                txtNote.TextChanged += this.Note_TextChanged;
                txtNote.SelectionChanged += this.txtNote_SelectionChanged;
            }
        }

      
        /// <summary>
        /// Lookup ArchivedNoteNode in tree by search result item ID
        /// </summary>
        /// <param name="nodes">Nodes at archive tree</param>
        /// <param name="id">Selected search item ID</param>
        /// <returns>Node from archive tree to be shown</returns>
        private ArchivedNoteNode LookUpArchivedNode(TreeNodeCollection nodes, long id)
        {
            ArchivedNoteNode archNode;
            ArchivedNoteNode resultNode = null;
            foreach(TreeNode n in nodes){
                if (n is CategoryNode)
                {
                   resultNode = LookUpArchivedNode(n.Nodes, id);
                   if (null != resultNode)
                       return resultNode;
                }
                else // it is ArchivedNoteNode
                {
                    archNode = (ArchivedNoteNode)n;
                    //Logger.WriteLine("Looking up node ID: " + archNode.ID.ToString() + ", :" + archNode.Text);
                    if (id == (archNode.ID))
                        return archNode;
                }
            }
            return null;
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 0)
            { //selected 1st tab = Archive
                
                treeArchive.Select();
                // After swich from search select last selected item in tree as well.
                if (null != treeArchive.SelectedNode)
                {
                    if (!treeArchive.SelectedNode.IsVisible)
                        treeArchive.SelectedNode.EnsureVisible();
                }
                /*if (!treeArchive.SelectedNode.Parent.IsExpanded)
                {
                    TreeNode parent = treeArchive.SelectedNode.Parent;
                    while (null != parent){
                        parent.Expand();
                        parent = parent.Parent;
                    }
                }*/

            }
        }

        

        private void ArchiveForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.N:
                        CreateNewNote();
                        e.SuppressKeyPress = true;
                        break;
                }
            }
        }

     
		
	}
}
